package com.yoo.ons.function.premium

import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.yoo.chat.kit.utils.StatusBarUtil
import com.yoo.ons.adapter.PremiumCycleAdapter
import com.yoo.ons.adapter.PremiumSubscriptionsAdapter
import com.yoo.ons.base.BaseVMActivity
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.databinding.ActivityPremiumBinding
import com.yoo.ons.function.premium.viewmodel.PremiumViewModel
import com.yoo.ons.model.PremiumCycleModel
import com.yoo.ons.model.PremiumModel

/**
 * 订阅页面
 */
class PremiumActivity:BaseVMActivity<ActivityPremiumBinding,PremiumViewModel>(ActivityPremiumBinding::inflate,PremiumViewModel::class.java) {

    override fun initView() {
        super.initView()
        binding.titleBar.setTitleText(getString(com.yoo.module_res.R.string.premium))
        binding.titleBar.setTitleBarPadding(StatusBarUtil.getStatusBarHeight(this))
        binding.titleBar.setBackListener { finish() }
    }

    override fun initData() {
        mViewModel?.getPremiumSubscription(object :HttpRequestListener<PremiumModel>(){
            override fun addRequestParams(params: HashMap<String, Any>) {

            }

            override fun success(t: PremiumModel?) {
                t?.apply {
                    //如果是会员，判断是否订阅
                    if (t.memberCode != null)
                        binding.txtPremiumExpireDate.text = if (t.autoRenew == 1) getString(com.yoo.module_res.R.string.auto_subscription) else "Until: ${t.expiryDate}"

                    binding.txtPremiumTitle.text=t.memberDescription.tip
                    binding.txtPremiumEquitySize.text=t.memberDescription.content
                    binding.txtHeadTips.isVisible= t.memberDescription.header!=null && t.memberDescription.header!=""
                    binding.txtHeadTips.text=t.memberDescription.header

                    //会员周期选择
                    binding.cycleList.layoutManager=GridLayoutManager(this@PremiumActivity,3)
                    val premiumCycleAdapter = PremiumCycleAdapter(object : PremiumCycleAdapter.OnListener {
                            override fun onListener(item: PremiumCycleModel) {

                            }

                        })
                    val data = t.productDescriptions.reversed()
                    premiumCycleAdapter.addAll(data as ArrayList<PremiumCycleModel>)
                    binding.cycleList.adapter= premiumCycleAdapter

                    //会员权益介绍
                    binding.premiumSubscriptionsList.layoutManager=LinearLayoutManager(this@PremiumActivity)
                    val premiumSubscriptionsAdapter = PremiumSubscriptionsAdapter()
                    premiumSubscriptionsAdapter.addAll(t.subscriptions)
                    binding.premiumSubscriptionsList.adapter= premiumSubscriptionsAdapter
                }
            }

        })

    }


    companion object{
        fun start(context :Context?){
            val intent = Intent(context, PremiumActivity::class.java)
            context?.startActivity(intent)
        }
    }
}